package com.dayaokeji.rhythmschoolstudent.client.home.meeting;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseFragment;
import com.dayaokeji.rhythmschoolstudent.client.main.MainFragment;
import com.dayaokeji.rhythmschoolstudent.event.UpdateMeetingEvent;
import com.dayaokeji.rhythmschoolstudent.utils.DialogUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.MeetingApi;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;

/**
 *  会议
 */
public class MeetingFragment extends BaseFragment implements MainFragment{

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private static final MeetingApi meetingApi = ApiUtils.getApi(MeetingApi.class);
    private Call<ServerResponse<Void>> addMeetingCall;

    @Nullable
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_meeting,container,false);
    }

    @Override
    public Toolbar getToolbar() {
        return toolbar;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        setHasOptionsMenu(true);
        setFragment();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.meeting_list,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_meeting:
                addMeeting();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setFragment() {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content,MeetingListFragment.newInstance());
        fragmentTransaction.commit();
    }

    /**
     * 加入会议
     */
    private void addMeeting() {
        final AppCompatDialog appCompatDialog = DialogUtils.getAppCompatDialog(getActivity(), R.layout.dialog_add);
        if (appCompatDialog == null) return;
        final TextView dialogTitle = (TextView) appCompatDialog.findViewById(R.id.tv_dialog_title);
        final EditText userInput = (EditText) appCompatDialog.findViewById(R.id.et_user_input);
        final Button btnSubmit = (Button) appCompatDialog.findViewById(R.id.btn_submit);
        dialogTitle.setText("添加会议");
        userInput.setHint("请输入会议ID");
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String meetingId = userInput.getText().toString().trim();
                if (!TextUtils.isEmpty(meetingId)) {
                    addMeetingHandle(meetingId);
                    if (appCompatDialog != null) {
                        appCompatDialog.dismiss();
                    }
                } else {
                    ToastUtils.info("请输入会议ID");
                }
            }
        });

    }

    /**
     * 加入会议处理
     * @param meetingId
     */
    private void addMeetingHandle(String meetingId) {
        final Map<String, Object> params = new HashMap<>();
        params.put("id", meetingId);
        params.put("userId", UserUtils.getUserInfo().getId());
        addMeetingCall = meetingApi.addMeeting(params);
        addMeetingCall.enqueue(new ServerCallback<Void>(getActivity(), "加入会议中...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    EventBus.getDefault().post(new UpdateMeetingEvent());
                    ToastUtils.success("加入成功");
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (addMeetingCall != null) {
            addMeetingCall.cancel();
        }
        super.onDestroy();
    }
}
